//
//  HDArchiveDetailTableViewCell.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/5/17.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


protocol HDArchiveDetailDelegate:class {
    
    func showBigImageClickDelegate(_ sender:HDImageBtn)

}

class HDArchiveDetailTableViewCell: UITableViewCell {
    weak var delegate:HDArchiveDetailDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    var detailModel:HDArchiveDetailModel? {
        didSet{
            createIconLabel()
            createLine()
            if self.detailModel?.pictureArray.count>0 {
                for index in 0..<self.detailModel!.pictureArray.count{
                    
                    createBtnImageView(index , section: (detailModel?.section)!, model: (detailModel?.pictureArray[index])!)
                }
            }
            
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
    func createIconLabel(){
        let icon_imageView = UIImageView()
        contentView.addSubview(icon_imageView)
        icon_imageView.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.left.equalTo(25)
            make.width.equalTo(7)
            make.height.equalTo(7)
        }
        icon_imageView.image = UIImage(named: "分类标识")
        let titleLabel = UILabel()
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.left.equalTo(icon_imageView.snp.right).offset(5)
            make.width.equalTo(200)
            make.height.equalTo(14)
            
        }
        titleLabel.text = backTheDataName((detailModel?.nameStr)!)
        titleLabel.font = UIFont.systemFont(ofSize: 14)
        titleLabel.textAlignment = .left
        titleLabel.textColor = UIColor(hex:0x555555)
    }
    
    func createBtnImageView(_ number: Int,section:NSInteger,model:HDPayoutCaseInitModel)->HDImageBtn{
        
        let index_X = number%4
        let index_Y = number/4
        let imageHeightOrWith = Int(AppConstant.screenWidth - 20*5)/4
        let imageBtn = HDImageBtn()
        imageBtn.selectIndexPath = section
        imageBtn.selectImageNumber = number
        imageBtn.layer.cornerRadius = 2.5
        imageBtn.layer.masksToBounds = true
        imageBtn.btnImageView = UIImageView()
        
        self.contentView.addSubview(imageBtn)
        
        imageBtn.snp.makeConstraints { (make) in
            make.top.equalTo(30 + (imageHeightOrWith + 20) * index_Y)
            make.left.equalTo(10 + 20 + (imageHeightOrWith + 20) * index_X)
            make.height.equalTo(imageHeightOrWith)
            make.width.equalTo(imageHeightOrWith)
        }
        
        
        if detailModel!.imageArray[number].image != nil {
            imageBtn.btnImageView?.image = detailModel!.imageArray[number].image
        }
        else{
            
            imageBtn.btnImageView!.sd_setImage(with: URL(string: model.img_small)!, placeholderImage: UIImage(named: "图片默认图"),options: .retryFailed, completed:{ (image, error, cacheType, url) in
                if image != nil{
                    
                    HDSaveImage.saveImageWithImageName(image!, caseId: self.detailModel!.caseid, dataType: (self.detailModel?.nameStr)!, imageCount: number)
                }
            })
            
        }
        imageBtn.contentMode = .scaleAspectFit
        imageBtn.addTarget(self, action: #selector(HDArchiveDetailTableViewCell.showBigImageClick(_:)), for: .touchUpInside)
        
        return imageBtn
    }
    
    func showBigImageClick(_ sender:HDImageBtn){
        delegate?.showBigImageClickDelegate(sender)
        
    }
    
    
    func createLine(){
        let lineLabel = UILabel()
        contentView.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(18)
            make.width.equalTo(1)
            make.bottom.equalTo(0)
        }
        lineLabel.backgroundColor = UIColor(hex:0xd5d5d5)
    }

}
